package cn.itrip.biz.controller;

import cn.itrip.beans.dto.Dto;
import cn.itrip.beans.pojo.*;
import cn.itrip.beans.vo.CommentVo;
import cn.itrip.beans.vo.ItripHotelOrderExtend;
import cn.itrip.beans.vo.QueryComment;
import cn.itrip.biz.service.QueryHotelInfoService;
import cn.itrip.common.DtoUtils;
import cn.itrip.common.RedisUtil;
import cn.itrip.common.ValiditorCodeUtil;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("/api")
public class HotelInformationController {
    @Resource
    private QueryHotelInfoService queryHotelInfoService;
    @Resource
    private RedisUtil redisUtil;

    @RequestMapping(value = "/comment/getcommentlist", method = RequestMethod.POST)
    @ApiOperation(value = "获取评论", httpMethod = "POST")
    public Dto getCommentList(@RequestBody QueryComment queryComment) {
        try {
            return queryHotelInfoService.getCommentList(queryComment);
        } catch (Exception e) {
            return DtoUtils.getExcetpionDto(e,"");
        }
    }

    @RequestMapping(value = "/comment/gethotelscore/{id}", method = RequestMethod.GET)
    @ApiOperation(value = "获取酒店评分", httpMethod = "GET")
    public Dto getHotelScore(@PathVariable("id") String id) {
        try {
            return queryHotelInfoService.getHotelScore(id);
        } catch (Exception e) {
            return DtoUtils.getExcetpionDto(e,"");
        }
    }

    @RequestMapping(value = "/hotelroom/queryhotelroombed", method = RequestMethod.GET)
    @ApiOperation(value = "根据parentid  获取床型列表", httpMethod = "GET")
    public Dto queryHotelRoombed()  {
        try {
            return queryHotelInfoService.queryHotelRoombed();
        } catch (Exception e) {
            return DtoUtils.getExcetpionDto(e,"");
        }
    }

    @RequestMapping(value = "/comment/gettraveltype", method = RequestMethod.GET)
    @ApiOperation(value = "获取出游类型列表", httpMethod = "GET")
    public Dto getTravelType() {
        try {
            return queryHotelInfoService.getTravelType();
        } catch (Exception e) {
            return DtoUtils.getExcetpionDto(e,"");
        }
    }

    @RequestMapping(value = "/comment/getcount/{id}", method = RequestMethod.GET)
    @ApiOperation(value = "获取评论数量", httpMethod = "GET")
    public Dto getcount(@PathVariable("id") String id) {
        try {
            return queryHotelInfoService.getCount(id);
        } catch (Exception e) {
            return DtoUtils.getExcetpionDto(e,"");
        }
    }

    @RequestMapping(value = "//hotelroom/queryhotelroombyhotel", method = RequestMethod.POST)
    @ApiOperation(value = "根据条件查询酒店房间", httpMethod = "POST")
    public Dto queryHotelRoomByHotel(@RequestBody ItripHotelRoom itripHotelRoom)  {
        try {
            return queryHotelInfoService.selectByQuery(itripHotelRoom);
        } catch (Exception e) {
            return DtoUtils.getExcetpionDto(e,"");
        }
    }

    @RequestMapping(value = "/comment/getimg/{id}", method = RequestMethod.GET)
    @ApiOperation(value = "获取评价图片", httpMethod = "GET")
    public Dto getCommentImg(@PathVariable("id") String id)  {
        try {
            return queryHotelInfoService.getCommentImg(id);
        } catch (Exception e) {
            return DtoUtils.getExcetpionDto(e,"");
        }
    }

    @RequestMapping(value = "/hotelroom/getimg/{id}", method = RequestMethod.GET)
    @ApiOperation(value = "根据酒店房型ID查 询酒店房型图片", httpMethod = "GET")
    public Dto getHotelRoomImg(@PathVariable("id") String id)  {
        try {
            return queryHotelInfoService.getHotelRoomImg(id);
        } catch (Exception e) {
            return DtoUtils.getExcetpionDto(e,"");
        }
    }

    @RequestMapping(value = "/hotelorder/getpreorderinfo", method = RequestMethod.POST)
    @ApiOperation(value = "获取预订单信息", httpMethod = "POST")
    public Dto getPreorderInfo(@RequestBody ItripHotelOrder itripHotelOrder) {

        try {
            return queryHotelInfoService.getPreorder(itripHotelOrder);
        }catch (Exception e){
            return DtoUtils.getExcetpionDto(e,"");
        }

    }

    @RequestMapping(value = "/hotelorder/addhotelorder", method = RequestMethod.POST)
    @ApiOperation(value = "添加订单", httpMethod = "POST", protocols = "http", notes = "添加订单", response = Dto.class)
    public Dto addhotelorder(@RequestBody ItripHotelOrder itripHotelOrder,HttpServletRequest request) {
        try {
            String token= request.getHeader("token");
            return queryHotelInfoService.addhotelorder(itripHotelOrder,token);
        }catch (Exception e){
            return DtoUtils.getExcetpionDto(e,"");
        }
    }

    @RequestMapping(value = "/hotelorder/queryOrderById/{orderid}", method = RequestMethod.GET)
    @ApiOperation(value = "根据订单id获取详细信息", httpMethod = "GET", protocols = "http", notes = "获取详细信息", response = Dto.class)
    public Dto queryOrderById(@PathVariable String orderid) {
        try {
            return queryHotelInfoService.queryOrderById(orderid);
        } catch (Exception e) {
            return DtoUtils.getExcetpionDto(e, "");
        }
    }

    @RequestMapping(value = "/comment/gethoteldesc/{id}", method = RequestMethod.GET)
    @ApiOperation(value = "获取酒店评论以降序", httpMethod = "GET")
    public Dto getHotelDesc(@PathVariable("id") String id)  {
        try {
            return queryHotelInfoService.getHotelDesc(id);
        } catch (Exception e) {
            return DtoUtils.getExcetpionDto(e,"");
        }
    }

    @RequestMapping(value = "/comment/upload", method = RequestMethod.POST)
    @ApiOperation(value = "上传图片", httpMethod = "POST")
    public Dto getUpload(MultipartFile file, HttpServletRequest request)  {
        //设置日期格式
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
        String data = df.format(new Date());
        String token = request.getHeader("token");
        //根据获取的key获取redis中对应的value
        String key = redisUtil.getValue(token);
        //把JSON转换成ItripUser对象
        ItripUser user = JSONObject.parseObject(key, ItripUser.class);
        //获取用户id
        long id = user.getId();
        //生成6位随机数
        String sjs = ValiditorCodeUtil.getCode(6);
        //文件夹是否为空
        if (!file.isEmpty()) {
            //文件名拼接
            String name = id + "-" + data + "-" + "-" + sjs;
            //定义文件保存的本地路径
            String localPath = request.getSession().getServletContext().getRealPath("/data1");
            //获得文件类型
            String filename = file.getOriginalFilename();
            //获取后缀
            String prefix = filename.substring(filename.lastIndexOf(".") + 1);
            //判断文件类型是否属于图片类型
            if (prefix.equals("png") || prefix.equals("jpg")) {
                File files = new File(localPath, name);
                try {
                    file.transferTo(files);
                } catch (IOException e) {
                    return DtoUtils.getErrorDto("文件格式不匹配", "1111");
                }
                //创建集合
                List imglist = new ArrayList();
                imglist.add(files);
                return DtoUtils.getSuccesDto(imglist);
            } else {
                return DtoUtils.getErrorDto("文件格式不匹配", "1111");
            }
        }
        return DtoUtils.getErrorDto("文件为空", "1111");
    }

    @RequestMapping(value = "/comment/add",method = RequestMethod.POST)
    @ApiOperation(value = "添加酒店评论",httpMethod ="POST")
    public Dto add(@RequestBody CommentVo commentVo, HttpServletRequest request) {
        try {
            return queryHotelInfoService.addComment(commentVo,request);
        } catch (Exception e) {
            return DtoUtils.getExcetpionDto(e,"");
        }
    }


}

